Page({
  data: {
    faultTypeOptions: [
      { id: 'dark', name: '不亮' },
  // 全局加载状态
  globalLoading: false,
  globalLoadingText: '服务事项加载中...',

  // 显示全局加载
  showGlobalLoading(text = '服务事项加载中...') {
    const app = getApp()
    app.showGlobalLoading({
      loadingText: text
    })
  },

  // 隐藏全局加载
  hideGlobalLoading() {
    const app = getApp()
    app.hideGlobalLoading()
  },

  // 更新全局加载状态
  updateGlobalLoading(data) {
    this.setData({
      globalLoading: data.globalLoading,
      globalLoadingText: data.globalLoadingText || this.data.globalLoadingText
    })
  },
      { id: 'flash', name: '忽明忽暗' },
      { id: 'damage', name: '灯罩/灯杆损坏' },
      { id: 'wire', name: '线路/配电箱问题' }
    ],
    faultType: 'dark',
    lampCode: '',
    locationText: '',
    desc: '',
    photos: [],
    canUpload: true,
    contactName: '',
    contactPhone: '',
    errors: {}
  },

  onLoad() {
    this.updateCanUpload()
  },

  onPickFaultType(e) {
    this.setData({ faultType: e.currentTarget.dataset.id })
  },

  onInputLampCode(e) {
    this.setData({ lampCode: e.detail.value })
  },

  onInputLocation(e) {
    const value = e.detail.value
    const errors = Object.assign({}, this.data.errors, { locationText: '' })
    this.setData({ locationText: value, errors })
  },

  // 定位功能已移除，保留手填位置

  onInputDesc(e) {
    const value = e.detail.value
    const errors = Object.assign({}, this.data.errors, { desc: '' })
    this.setData({ desc: value, errors })
  },

  onChooseImage() {
    const remain = Math.max(0, 3 - (this.data.photos || []).length)
    if (remain === 0) {
      wx.showToast({ title: '最多上传3张照片', icon: 'none' })
      return
    }
    wx.chooseMedia({
      count: remain,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const files = (res.tempFiles || []).map(f => f.tempFilePath)
        const photos = [...(this.data.photos || []), ...files]
        this.setData({ photos }, this.updateCanUpload)
      }
    })
  },

  onPreview(e) {
    const url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: this.data.photos || []
    })
  },

  onRemovePhoto(e) {
    const url = e.currentTarget.dataset.url
    const photos = (this.data.photos || []).filter(p => p !== url)
    this.setData({ photos }, this.updateCanUpload)
  },

  onInputName(e) {
    this.setData({ contactName: e.detail.value })
  },

  onInputPhone(e) {
    const value = e.detail.value
    const errors = Object.assign({}, this.data.errors, { contactPhone: '' })
    this.setData({ contactPhone: value, errors })
  },

  updateCanUpload() {
    const canUpload = ((this.data.photos || []).length < 3)
    this.setData({ canUpload })
  },

  validate() {
    const { locationText, desc, contactPhone } = this.data
    const errors = { locationText: '', desc: '', contactPhone: '' }
    if (!locationText) {
      errors.locationText = '请填写具体位置'
    }
    if (!desc || desc.trim().length < 5) {
      errors.desc = '请填写问题描述（不少于5字）'
    }
    if (contactPhone && !/^1\d{10}$/.test(contactPhone)) {
      errors.contactPhone = '请输入正确的手机号'
    }
    this.setData({ errors })
    return !errors.locationText && !errors.desc && !errors.contactPhone
  },

  onSubmit() {
    if (!this.validate()) return
    const payload = {
      type: this.data.faultType,
      lampCode: this.data.lampCode,
      locationText: this.data.locationText,
      desc: this.data.desc,
      photos: this.data.photos,
      contactName: this.data.contactName,
      contactPhone: this.data.contactPhone
    }
    // 提交成功
    setTimeout(() => {
      wx.showToast({ title: '已提交', icon: 'success' })
      setTimeout(() => wx.navigateBack({}), 800)
    }, 800)
  }
})


